﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using VoluntariadoMobileModel;
using System.Net;
using System.IO;
using System.Collections.ObjectModel;

namespace VoluntariadoMobileViewModel
{
    public class VoluntariadoGeolocalizadoViewModel : ViewModel
    {
        double _latitud;
        double _longitud;
        int _radio;
        string _busqueda;

        ICommand _GetVoluntariadosGeolocalizados;
        ICommand _GetVoluntariadosBuscado;

        public ObservableCollection<VoluntariadoGeolocalizado> VoluntariadosGeolocalizados { get; set; }
      
        public double Latitud
        {
            get { return this._latitud; }
            set
            {
                if (this._latitud != value)
                {
                    this._latitud = value;
                    this.RaisePropertyChanged("Latitud");
                }
            }
        }

        public double Longitud
        {
            get { return this._longitud; }
            set
            {
                if (this._longitud != value)
                {
                    this._longitud = value;
                    this.RaisePropertyChanged("Longitud");
                }
            }
        }

        public int Radio
        {
            get { return this._radio; }
            set
            {
                if (this._radio != value)
                {
                    this._radio = value;
                    this.RaisePropertyChanged("Radio");
                }
            }
        }

        public string Busqueda
        {
            get { return this._busqueda; }
            set
            {
                if (this._busqueda != value)
                {
                    this._busqueda = value;
                    this.RaisePropertyChanged("Busqueda");
                }
            }
        }

  
        public VoluntariadoGeolocalizadoViewModel(double latitud, double longitud, int radio)
        {
            _latitud = latitud;
            _longitud = longitud;
            _radio = radio;
            VoluntariadoGeolocalizado();
        }

        public ICommand GetVoluntariadoGeolocalizado
        {
            get
            {
                this._GetVoluntariadosGeolocalizados = new RelayCommand()
                {
                    CanExecuteDelegate = c => true,
                    ExecuteDelegate = c => VoluntariadoGeolocalizado()
                };
                return this._GetVoluntariadosGeolocalizados;
            }
        } 

        public ICommand GetVoluntariadoGeolocalizadoBuscado
        {
            get
            {
                this._GetVoluntariadosBuscado = new RelayCommand()
                {
                    CanExecuteDelegate = c => true,
                    ExecuteDelegate = c => BuscarVoluntariadoGeolocalizado(_busqueda)
                };
                return this._GetVoluntariadosBuscado;
            }
        }


        public void VoluntariadoGeolocalizado()
        {
            System.Uri myUri = new System.Uri(string.Format("http://{0}:{1}/VoluntariadoMobile/getOportunidades?lat={2}&lon={3}&radio={4}",Settings.Default.Host,Settings.Default.Port, _latitud, _longitud, _radio));
            HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
            NetworkCredential myCred = new NetworkCredential("demo", "demo");
            CredentialCache myCredentialCache = new CredentialCache();
            myCredentialCache.Add(myUri, "Basic", myCred);
            HttpWRequest.Credentials = myCredentialCache;

            HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
            Stream strm = HttpWResponse.GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            VoluntariadosGeolocalizados = JsonHelper.JsonDeserialize<ObservableCollection<VoluntariadoGeolocalizado>>(sr.ReadToEnd());
            strm.Close();
            sr.Close(); 
        }


        private void BuscarVoluntariadoGeolocalizado(string busqueda)
        {
            System.Uri myUri = new System.Uri(string.Format("http://{0}:{1}/VoluntariadoMobile/getOportunidadesBuscar/{2}",Settings.Default.Host,Settings.Default.Port,busqueda));
            HttpWebRequest HttpWRequest = (HttpWebRequest)WebRequest.Create(myUri);
            NetworkCredential myCred = new NetworkCredential("demo", "demo");
            CredentialCache myCredentialCache = new CredentialCache();
            myCredentialCache.Add(myUri, "Basic", myCred);
            HttpWRequest.Credentials = myCredentialCache;

            HttpWebResponse HttpWResponse = (HttpWebResponse)HttpWRequest.GetResponse();
            Stream strm = HttpWResponse.GetResponseStream();
            StreamReader sr = new StreamReader(strm);
            VoluntariadosGeolocalizados = JsonHelper.JsonDeserialize<ObservableCollection<VoluntariadoGeolocalizado>>(sr.ReadToEnd());
            strm.Close();
            sr.Close(); 
        }

        
      

    }
}
